using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminCompanyListing : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataObj CurPageDataObj = new DataObj();

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.Table_MessageBox.Visible = false;

        if (!IsPostBack)
        {
           
            this.SwitchLanguage();
            this.PageComponentSetup();

        } 
    }

    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView);     
        }
    }

    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortOrder_SelectedIndexChanged(sender, e);
    }

    protected void DropDownList_ByEntity_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.DropDownList_ByEntity.SelectedValue.Length > 0)
        {
            this.LoadData();
            this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
        }
    }

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // setup entity
        if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsPowerAdmin)
        {
            #region Power Admin
            using (EntityManager CurEntityManager = new EntityManager(this.CurrentPrivateSession))
            {
                MasterBaseCollection ResultList = CurEntityManager.GetAllEntity();
                if (ResultList != null)
                {
                    this.DropDownList_ByEntity.Items.Add(new ListItem("", ""));
                    for (int x = 0; x < ResultList.Count; x++)
                    {
                        this.DropDownList_ByEntity.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                    }
                }
                else
                {
                    // display error
                    this.CurLogManager.LogError("[GonzalesAdminClaimantsListing.PageComponentSetup] : Failed on CurEntityManager.GetAllEntity() : " + CurEntityManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);

                }
            }
            #endregion
        }
        else
        {
            #region Finance Admin

            using (UserManager CurManager = new UserManager(this.CurrentPrivateSession))
            {
                MasterBaseCollection ResultList = CurManager.GetEntityByFinanceAdmin(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID);
                if (ResultList != null)
                {
                    for (int x = 0; x < ResultList.Count; x++)
                    {
                        this.DropDownList_ByEntity.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                    }
                }
                else
                {
                    // display error
                    this.CurLogManager.LogError("[GonzalesAdminClaimantsListing.PageComponentSetup] : Failed on CurEntityManager.GetAllEntity() : " + CurManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                }
            }

            this.DropDownList_ByEntity_SelectedIndexChanged(null, null);

            #endregion
        }
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");

                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("CompanyID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("DisplayName"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Country"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("IsRep"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("CCPaymentInString"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ExpiryDate"));

                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("CompanyID"), this.CurPageDataObj.CompanyID));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DisplayName"), this.CurPageDataObj.DisplayName));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Country"), this.CurPageDataObj.Country));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("IsRep"), this.CurPageDataObj.IsRep));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("CCPaymentInString"), this.CurPageDataObj.CCPaymentInString));

                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DESC"), "DESC"));
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedListing.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage

    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }

    protected void ShowErrorMessage(ArrayList ErrorMessageList)
    {
        if (ErrorMessageList != null)
        {
            StringBuilder ErrorMessageBuilder = new StringBuilder("<ul>");

            for (int x = 0; x < ErrorMessageList.Count; x++)
            {
                ErrorMessageBuilder.AppendLine("<li>" + ErrorMessageList[x] + "</li>");
            }

            ErrorMessageBuilder.AppendLine("</ul>");

            this.Table_MessageBox.Visible = true;
            this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessageBuilder.ToString();
        }
    }

    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (CompanyManager CurCompanyManager = new CompanyManager(this.CurrentPrivateSession))
        {
            GonzalesCompanies CompanyCollection = CurCompanyManager.GetCompanyByEntityID(this.DropDownList_ByEntity.SelectedValue);

            if (CompanyCollection != null)
            {
                this.ResultTable = new DataTable();

                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.InternalID, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CompanyID, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DisplayName, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Manager, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Country, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CCPaymentInString, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.IsRep, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.ExpiryDate, Type.GetType("System.String")));
                //this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EditLink, Type.GetType("System.String")));

                foreach (GonzalesCompany CurCompany in CompanyCollection)
                {
                    DataRow ResultRow = this.ResultTable.NewRow();

                    ResultRow[this.CurPageDataObj.InternalID] = CurCompany.InternalID;
                    ResultRow[this.CurPageDataObj.CompanyID] = "<a href='GonzalesAdminCompanyDetail.aspx?id=" + CurCompany.InternalID + "'  class='GridViewLink'>" + CurCompany.CompanyCode + "</a>";// CurCompany.CompanyCode;
                    ResultRow[this.CurPageDataObj.DisplayName] = CurCompany.CompanyName;
                    ResultRow[this.CurPageDataObj.Manager] = CurCompany.CompanyManager.DisplayName;
                    ResultRow[this.CurPageDataObj.Country] = CurCompany.FromCountry.DisplayName;
                    ResultRow[this.CurPageDataObj.CCPaymentInString] = CreditCardManager.GetCreditCardPaymentTypeInString(CurCompany.CreditCardPaymentType); ;
                    ResultRow[this.CurPageDataObj.IsRep] = (CurCompany.IsRepresentative) ? "<img src='" + GonzalesImages.Icon_Checked + "'>" : "";
                    ResultRow[this.CurPageDataObj.ExpiryDate] = CurCompany.ExpiryDate;
                   // ResultRow[this.CurPageDataObj.EditLink] = "<a href='GonzalesAdminCompanyDetail.aspx?id=" + CurCompany.InternalID + "'  class='GridViewLink'>Edit</a>";

                    this.ResultTable.Rows.Add(ResultRow);
                }

            }
            else
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminCompanyListing.LoadData] : CurCompanyManager.GetCompanyByEntityID('" + this.DropDownList_ByEntity.SelectedValue + "') : " + CurCompanyManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage);
            }
        }
    }

    #endregion

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string InternalID = "InternalID";
        public readonly string CompanyID = "CompanyID";
        public readonly string DisplayName = "DisplayName";
        public readonly string Manager = "Manager";
        public readonly string Country = "Country";
        public readonly string IsRep = "IsRep";
        public readonly string CCPaymentInString = "CCPaymentInString";
        public readonly string ExpiryDate = "ExpiryDate";  
        public readonly string EditLink = "EditLink";

    }

    #endregion

}
